/**
 * Copyright (C) 2017 - 2020 https://github.com/joewee
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bifrost.entity;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Set;

/**
 * 权限
 *
 * @author joewee
 * @version 1.0.0
 * @Date 2018/2/25 21:41
 */
@Entity
@Table(name = "PERMISSION")
@NamedQuery(name = "Permission.findAll", query = "SELECT p FROM Permission p ")
@Getter
@Setter
@Builder
@AllArgsConstructor
public class Permission extends BaseEntity implements Serializable {

    @Id
    @Column(name = "PERMISSION_ID", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "PERMISSION_NAME", unique = true)
    private String permissionName;

    @Column(name = "PERMISSION_DESCRIPTION")
    private String permissionDescription;

    @Column(name = "URL")
    private String url;

    @Column(name = "MENU_URL")
    private String menuURL;

    @Column(name = "TYPE", columnDefinition = "tinyint")
    private int type;//权限的类型：0-目录 1-菜单 2-按钮

    @Column(name = "ICON")
    private String icon;//图标

    @Column(name = "IDENTIFIER", unique = true)
    private String identifier;//用于标识权限

    @Column(name = "SHOW_ORDER", columnDefinition = "tinyint")
    private int order;//权限相同层级下的排序顺序

    @ManyToOne
    @JoinColumn(name = "PARENT_ID", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
    @JsonBackReference//序列化时忽略此属性（和ignore的区别是反序列化的时候会设置该属性）
    private Permission parent;

    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    @OrderBy("order ASC")
    @JsonManagedReference//序列化时序列化子孩子属性
    private Set<Permission> children;

    @JsonBackReference
    @ManyToMany(mappedBy = "permissions")
    private Set<Role> roles;

    public Permission() {

    }

    public Permission(String permissionName) {
        this.permissionName = permissionName;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof Permission)) {
            return false;
        }
        Permission permission = (Permission) o;
        return permission.getId() == id;
    }

    @Override
    public int hashCode() {
        return this.permissionName.hashCode();
    }
}
